﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.IO;
using ScrumTable.Common;
using ScrumTable.Common.Config.Xml;
using ScrumTable.DL.Data.Common.Config.Driver;
using ScrumTable.DL.Data.Generic.Properties;

#endregion

namespace ScrumTable.DL.Data.Generic.Config.Driver
{
    /// <summary>
    /// Represents the base selector for all driver configuration files which
    /// are serialized by the current driver.
    /// </summary>
    public class GenericDriverConfigSource<T> : IDriverConfigSource
        where T : IDriverConfig, new()
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly T _configData;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the name of the driver configuration source. This property
        /// contains human readable information about the driver configuration
        /// in order to select the appropriate configuration source.
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Gets the deserialized diver configuration data which should
        /// be initialized by the underlying driver.
        /// </summary>
        public IDriverConfig SourceData { get { return _configData; } }

        /// <summary>
        /// Returns true if the current configuration can be saved back to
        /// file system.
        /// </summary>
        public bool CanSave
        {
            get { return !string.IsNullOrEmpty(FilePath); }
        }

        /// <summary>
        /// Gets the file system location of the driver configuration.
        /// </summary>
        private string FilePath { get; set; }

        #endregion

        #region Constructors / Destructor

        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="GenericDriverConfigSource{T}"/> class.
        /// </summary>
        public GenericDriverConfigSource(T sourceData, string filePath)
        {
            PreCondition.AssertNotNull(sourceData, "sourceData");
            PreCondition.AssertNotNull(filePath, "filePath");

            _configData = sourceData;

            Name = (!string.IsNullOrEmpty(filePath)) ? Path.GetFileNameWithoutExtension(filePath) : sourceData.SectionName;
            FilePath = filePath;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Saves the driver configuration back to the file system.
        /// </summary>
        public void Save()
        {
            if (!CanSave) { throw new InvalidOperationException(Resources.ExcGenDrvConfigSaveNotSupported); }

            new XmlConfigSerializer<T>().SerializeFile(FilePath, _configData);
        }

        #endregion

        #region Events

        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
